package com.example.util;

import com.example.entity.DayHourChargeConfig;
import com.example.entity.ParkingFeeConfig;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Optional;

public class HourlyRateCalculator {

    public static BigDecimal calculateHourlyRate(double hours, DayHourChargeConfig config) {
        return Optional.ofNullable(config.getHourlyRates())
                .flatMap(rates -> rates.stream()
                        .filter(rate -> rate.getHours() <= hours)
                        .max(Comparator.comparingInt(DayHourChargeConfig.HourlyRate::getHours))
                        .map(DayHourChargeConfig.HourlyRate::getRate))
                .map(rate -> rate.multiply(BigDecimal.valueOf(Math.ceil(hours))))
                .orElse(BigDecimal.ZERO);
    }
}